import Studycodemodel from "../mysql_interface/studycodemodel";
import M from "markdown-it";
// import mdMathJax from 'markdown-it-mathjax-chtml';
import usem from 'markdown-it-mathjax3'
// const katexAutoRender = (window as any).renderMathInElement;

export function escapeHtml(text?: string) {
  const map: Record<string, string> = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': "&quot;",
    "'": "&#039;",
  };

  return text
    ? text.replace(/[&<>"']/g, function (m) {
      return map[m];
    })
    : "";
}

function returnhtml(str?: string) {
  if (!str) return ''
  // const doc = document.createElement('div');
  // doc.innerHTML = str;
  // const data = await (window as any).MathJax.tex2mml(str);
  // (window as any).doc123 = doc;
  return str
}

export function markdownhtml(
  e: Studycodemodel.Iquestion | Studycodemodel.Imatchquestion
): unknown {
  const mt = M({
    html: true,
    linkify: true,
    typographer: true,
    breaks: true,
  }).use(usem)
  if (e.context) {
    e.context = mt.render(returnhtml(e.context));
  }
  if (e.background) {
    e.background = mt.render(returnhtml(e.background));
  }
  if (e.inputformat) {
    e.inputformat = mt.render(returnhtml(e.inputformat));
  }
  if (e.outputformat) {
    e.outputformat = mt.render(returnhtml(e.outputformat));
  }
  if (e.tips) {
    e.tips = mt.render(returnhtml(e.tips));
  }
  return e;
}

function questionhtml(x?: NodeListOf<HTMLDivElement>) {
  const e = (window as any).MathJax.startup.defaultPageReady().then(() => {

  }).catch((e: any) => {
    console.log(e)
  })

}

export default questionhtml;
